
	import PComm.TComm;
	
	import PDatabase.TDatabase;
	
	import TController.Controller;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.controls.List;
	import mx.core.Application;
	import mx.events.CloseEvent;
	import mx.events.ListEvent;
	
	private var dockImage:BitmapData;
	public var controller:Controller;
	

	public function init():void
	{
		

		controller=new Controller();		
		/*controller.database.openDatabase((File.applicationDirectory.resolvePath("guardianDatabase.db")as File).nativePath);
		Engine.permaban=controller.database.getRecord("SELECT * FROM apps WHERE permabanned==\"1\"",new Array("appname","brutalize","permabanned","tag","time","pattern"));
		Engine.permacool=controller.database.getRecord("SELECT * FROM apps WHERE permacool==\"1\"",new Array("appname","brutalize","permabanned","tag","time","pattern"));		 
		 */
		controller.comm=new TComm();
		controller.app=Application.application;
		BindingUtils.bindProperty(newFilenameTextInput,"text",controller,"pattern");
		//controller.comm.addEventListener(URLloadingEvent.DONELOAD,controller.eventHandling)
		//controller.databaseFile=new File();
		//controller.databaseFile.addEventListener(Event.SELECT,onSelectfile);
		//controller.progressEdit=progressEdit;
		
		
		
		//openNewWindow(tt.win);
		//Use the loader object to load an image, which will be used for the systray       //After the image has been loaded into the object, we can prepare the application       //for docking to the system tray 
    
      /*var loader:Loader = new Loader();
      loader.contentLoaderInfo.addEventListener(Event.COMPLETE, prepareForSystray);
      loader.load(new URLRequest("assets/iconangel2.png"));*/
       
      //Catch the closing event so that the user can decide if it wants to dock or really       //close the application 
      //this.addEventListener(Event.CLOSING, closingApplication);
	  
	
	};
	
	public function browseButtonClick(e:Event):void
	{
		controller.browseForFolder();
	}
	
	public function getDataButtonClick(e:Event):void
	{
		controller.getData();
	}
	
	public function onListClick(e:ListEvent):void
	{
		var t:List=e.currentTarget as List;
		controller.showInfo(t.selectedIndex);		
	}
	/*public function onDbButtons(evt:Event):void
	{
		
		
		dbAppsBT.setStyle("color","#AAAAAA");
		dbProjectsBT.setStyle("color","#AAAAAA");
		dbTagsBT.setStyle("color","#AAAAAA");
		
		if(evt.target.label == "Apps"){
			controller.loadDbToGrid("apps");
		}
		if(evt.target.label == "Projects"){
			controller.loadDbToGrid("projects");
		}
		if(evt.target.label == "Tags"){
			controller.loadDbToGrid("tags");	
		}
		(evt.target as Button).setStyle("color","#FFFFFF");
	}
	*/
	public function moveForm(e:MouseEvent):void
	{
		//e.stopPropagation();
		//trace(e);
		if((e.target.toString().indexOf("mainWindow.UIComponent")>0)||(e.target.toString().indexOf("mainWindow.border")==99))
		//if(isResizing!=true)
		  	{
	   		this.stage.nativeWindow.startMove();
	  		}
	}
	
	
	public function closeForm():void
	{
	trace("closing/leaving app");
	//controller.closeWindows();
	//delete tt5;
	
	/*
	if(controller.rememberMe==true){
		CPreferencesManager.setPreference("rememberMe",true)
		CPreferencesManager.setPreference("name",controller.name)
		CPreferencesManager.setPreference("password",controller.password)
	}
	else {
		CPreferencesManager.setPreference("rememberMe",false)
		CPreferencesManager.setPreference("name","")
		CPreferencesManager.setPreference("password","")
	}
	
	CPreferencesManager.setPreference("mainWindowWidth",mainWindow.width);
	CPreferencesManager.setPreference("mainWindowHeight",mainWindow.height);*/	
	 
	 //Don't close, so prevent the event from happening 
       
     ///Check what the user really want's to do       
     //Alert.buttonWidth = 110; 
      /*Alert.yesLabel = "Close";
      Alert.noLabel = "Minimize";
      
      Alert.show("Close or minimize?", "Close?", 3, this, alertCloseHandler);*/
      	

      	Application.application.close();
      	
      	
      	NativeApplication.nativeApplication.exit(0);
	}
	
	private function alertCloseHandler(event:CloseEvent):void {
      if (event.detail==Alert.YES) {
      	var t:Array=new Array();
			t[0]=new Array();
			t[0][0]="command";
			t[0][1]="exit";
			//controller.comm.sendRequest("http://127.0.0.1:38561","POST",t);
        	Application.application.close();
      } else {
         dock();
      }
   }

public function prepareForSystray(event:Event):void {
    
      //Retrieve the image being used as the systray icon 
      dockImage = event.target.content.bitmapData;
       
      //For windows systems we can set the systray props       
      //(there's also an implementation for mac's, it's similar and you can find it on the net... ;) ) 
      if (NativeApplication.supportsSystemTrayIcon){
         setSystemTrayProperties();
          
         //Set some systray menu options, so that the user can right-click and access functionality          
         //without needing to open the application          
SystemTrayIcon(NativeApplication.nativeApplication .icon).menu = createSystrayRootMenu();
      }
   }

private function createSystrayRootMenu():NativeMenu{
      //Add the menuitems with the corresponding actions 
      var menu:NativeMenu = new NativeMenu();
      var openNativeMenuItem:NativeMenuItem = new NativeMenuItem("Open");
      var exitNativeMenuItem:NativeMenuItem = new NativeMenuItem("Exit");

      //What should happen when the user clicks on something...       

	openNativeMenuItem.addEventListener(Event.SELECT, undock);

     // exitNativeMenuItem.addEventListener(Event.SELECT, closeApp);

      //Add the menuitems to the menu 
      menu.addItem(openNativeMenuItem);
      menu.addItem(new NativeMenuItem("",true));
//separator 
      menu.addItem(exitNativeMenuItem);
       
      return menu;
   }

	
	private function setSystemTrayProperties():void{
      //Text to show when hovering of the docked application icon       
SystemTrayIcon(NativeApplication.nativeApplication .icon).tooltip = "The Guardian Angel";
       
      //We want to be able to open the application after it has been docked       
SystemTrayIcon(NativeApplication.nativeApplication .icon).addEventListener(MouseEvent.CLICK, undock);
       
      //Listen to the display state changing of the window, so that we can catch the minimize       
stage.nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING, nwMinimized); //Catch the minimize event 
   }
    private function nwMinimized(displayStateEvent:NativeWindowDisplayStateEvent):void {
    
      //Do we have an minimize action?       //The afterDisplayState hasn't happened yet, but only describes the state the window will go to,       //so we can prevent it! 
      if(displayStateEvent.afterDisplayState == NativeWindowDisplayState.MINIMIZED) {
         //Prevent the windowedapplication minimize action from happening and implement our own minimize          //The reason the windowedapplication minimize action is caught, is that if active we're not able to          //undock the application back neatly. The application doesn't become visible directly, but only after clicking          //on the taskbars application link. (Not sure yet what happens exactly with standard minimize) 
         displayStateEvent.preventDefault();
          
         //Dock (our own minimize) 
         dock();
      }
   }
   
    public function dock():void {
      //Hide the applcation 
      stage.nativeWindow.visible = false;
       
      //Setting the bitmaps array will show the application icon in the systray 
      NativeApplication.nativeApplication .icon.bitmaps = [dockImage];
   }
   public function undock(evt:Event):void {
      //After setting the window to visible, make sure that the application is ordered to the front,       //else we'll still need to click on the application on the taskbar to make it visible 
      stage.nativeWindow.visible = true;
      stage.nativeWindow.orderToFront();
       
      //Clearing the bitmaps array also clears the applcation icon from the systray 
      NativeApplication.nativeApplication .icon.bitmaps = [];
     // controller.undocked();
   }




	
